package client;
import java.util.ArrayList;

import javax.persistence.*;
import javax.ejb.*;

import agent.Agent;

/**
 * EJB de gestion des clients
 * @author Geoffroy
 *
 */
@Stateless
@Local
public class ClientEJB implements ClientInterface {

	
	@PersistenceContext
	EntityManager em;
	
	public void addClient(Client c) {
		if(em!=null) {
			em.persist(c);
		}
	}
	
	public void update(Client c) {
		em.flush();
	}
	
	public Client findById(int id) {
		return (Client)em.find(Client.class,id);
	}
	
	public void remove(int idClient) {
		em.remove(this.findById(idClient));
	}
	
	public ArrayList<Client> findAll() {
		Query q = em.createQuery("SELECT a FROM Client a");
		return (ArrayList<Client>)q.getResultList();
	}
	public Client findByUserId(int uid) {
		Query q = em.createQuery("SELECT a FROM Client a WHERE a.utilisateur.id="+uid);
		return (Client)q.getSingleResult();
	}
}
